﻿
namespace Patterns4Net.ArchitectureExplorer.Documentation
{
    using System.ComponentModel.Composition;
    using System.Xml.Linq;
    using System.Xml.XPath;
    using Attributes;

    [Export(typeof(IPatternDocProvider))]
    public class XmlPatternDocProvider : IPatternDocProvider
    {
        private readonly XDocument config;

        private XmlPatternDocProvider(XDocument config)
        {
            this.config = config;
        }

        public static XmlPatternDocProvider Create(XDocument config)
        {
            // here we leave some place for possible validation against xsd, 
            // which is, however, not implemented at the moment...
            return new XmlPatternDocProvider(config);
        }

        public XElement GetDocumentation(IPattern pattern)
        {
            var xpath = string.Format("//pattern[class/@full-name='{0}']/description", pattern.GetType().FullName);
            return this.config.XPathSelectElement(xpath);
        }
    }
}
